package ibm.cognos.imt


import ibm.cognos.imt.chart.PieChart
import ibm.cognos.imt.dataUsage.CrosstabColumn
import ibm.cognos.imt.dataUsage.CrosstabRow
import ibm.cognos.imt.dataUsage.Measure

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 2/8/14
 * Time: 12:08 PM
 * To change this template use File | Settings | File Templates.
 */
class Crosstab {
    def queryName
    def name
    def measure
    def column
    def row
    def dataItems
    def owner

    Crosstab(){}

    Crosstab(report, items) {
        owner = report
        dataItems = items
        items.each {
//            println it.getDataItem().name
            if (it instanceof CrosstabColumn) {
                this.column = it.getDataItem().name
            }
            if (it instanceof CrosstabRow) {
                this.row = it.getDataItem().name
            }
            if (it instanceof Measure) {
                this.measure = it.getDataItem().name
//                it.getDataItem().aggregate="total"
            }
        }
    }

    Crosstab (node){
        name = node.@name
        queryName = node.@refQuery
        measure = node.defaultMeasure.@refDataItem
        column = node.crosstabColumns.crosstabNode.crosstabNodeMembers.crosstabNodeMember.@refDataItem
        row = node.crosstabRows.crosstabNode.crosstabNodeMembers.crosstabNodeMember.@refDataItem
    }

    def toChart() {
        PieChart chart = new PieChart()
        chart.owner = owner
        chart.name = name
        chart.measure = measure
        chart.queryName = queryName
        chart.slices = column
        chart.categories = row
        chart
    }

    def generateSpec(builder) {
        builder.crosstab(refQuery: "${queryName}",name: "${getName()}", horizontalPagination: "true") {
            crosstabCorner {
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "xm")

                    }
                }
                contents {
                    textItem {
                        dataSource {
                            dataItemLabel(refDataItem: "${measure}")
                        }
                    }
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "xt")
                }
                CSS(value: "border-collapse:collapse")
            }
            defaultMeasure(refDataItem: "${measure}")
            crosstabFactCell {
                contents {
                    textItem {
                        dataSource {
                            cellValue()
                        }
                    }
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "mv")
                    }
                }
            }
            crosstabColumns {
                crosstabNode {
                    crosstabNodeMembers {
                        crosstabNodeMember(refDataItem: "${column}", edgeLocation: "e1") {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "ml")
                                }
                            }
                            contents {
                                textItem {
                                    dataSource {
                                        memberCaption()
                                    }
                                }
                            }
                        }
                    }
                }
            }
            crosstabRows {
                crosstabNode {
                    crosstabNodeMembers {
                        crosstabNodeMember(refDataItem: "${row}", edgeLocation: "e2") {
                            style {
                                defaultStyles {
                                    defaultStyle(refStyle: "ml")
                                }
                            }
                            contents {
                                textItem {
                                    dataSource {
                                        memberCaption()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


    }
}
